package com.oocl.homework0711.scanner.imp;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.oocl.homework0711.customer.Customer;
import com.oocl.homework0711.scanner.DateScanner;

public class DateScannerImp implements DateScanner {
	private InputStream in;
	private Scanner scanner;
	private String regex = "^\\d+([\\s\\t]+)"
			+ "[a-zA-Z]+([\\s\\t]+)"
			+ "[a-zA-Z0-9_\\-\\.]+@[a-zA-Z0-9]+(\\.(com|cn|org|edu|hk))([\\s\\t]+)"
			+ "(true|false)([\\s\\t]+)"
			+ "(((199\\d)|(20\\d{2}))/((0*\\d)|(1[0-2]))/((0*\\d)|([1-2]\\d)|(3[01])))|(((199\\d)|(20\\d{2}))-((0*\\d)|(1[0-2]))-((0*\\d)|([1-2]\\d)|(3[01])))$";
	
	
	public DateScannerImp(InputStream in) {
		super();
		this.in = in;
		this.scanner=new Scanner(in);
	}
	public List<Customer> scann() {
		List<String> list=preScan(regex);
		List<Customer> list1=new ArrayList<Customer>();
		String reg="[\\s\\t]+";
		for(String s:list){
			String[] ss=s.split(reg);
			Customer c=new Customer();
			c.setId(Integer.parseInt(ss[0]));
			c.setName(ss[1]);
			c.setEmail(ss[2]);
			c.setSex(ss[3]);
			c.setBirthday(ss[4]);
			list1.add(c);
		}
		
		return list1;
	}
	private List<String> preScan(String regex){
		List<String> list=new ArrayList<String>();
		Pattern pattern;
		Matcher matcher;
		while(scanner.hasNext()){
			String string= scanner.nextLine();
			pattern  = Pattern.compile(regex);
			matcher = pattern.matcher(string);
			if(matcher.find())
				list.add(string);
		}
		return list;
	}

}
